package com.zzsy.carosshopprojects.activitys.home.mainpage.common.payresult;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.zzsy.carosshopprojects.R;
import com.zzsy.carosshopprojects.base.BaseFragment;
import com.zzsy.carosshopprojects.bean.OrderDetialBean;
import com.zzsy.carosshopprojects.util.AlgorithmUtil;
import com.zzsy.carosshopprojects.util.sunmiprint.PrintHelper;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 支付成功
 */
public class PaySuccessFragment extends BaseFragment {
    public static PaySuccessFragment newInstance(String orderNumber, int kind) {
        PaySuccessFragment paySuccessFragment = new PaySuccessFragment();
        Bundle bundle = new Bundle();
        bundle.putString("orderNumber", orderNumber);
        bundle.putInt("kind", kind);
        paySuccessFragment.setArguments(bundle);
        return paySuccessFragment;
    }

    @BindView(R.id.tv_print_two)
    TextView tvPrintTwo;
    @BindView(R.id.tv_payState)
    TextView tv_payState;
    @BindView(R.id.tv_order_money)
    TextView tvOriderMoney;
    private int kind = 0;
    private PayResultMode payResultMode;
    private OrderDetialBean orderDetialBean;

    @Override
    protected int initLayout() {
        return R.layout.fg_paysuccess;
    }

    @Override
    protected void initView(View view) {
        initTitle(view, getResources().getString(R.string.paySuccess), getActivity());
        tv_payState.setText("付款成功");
       // tvPrintTwo.setBackground(ViewBgUtil.setBg(360, Color.parseColor(ConfigureFile.ColorSystem)));
    }

    @Override
    protected void initData(Context mContext) {
        payResultMode = getViewModelProvider().get(PayResultMode.class);
        kind = getArguments().getInt("kind", 0);
    }

    @Override
    protected void loadMethod() {
        getOrderDetail();
    }

    /**
     * 获取订单详情
     */
    private void getOrderDetail() {
        payResultMode.getLiveData().observe(this, orderDetialBean -> {
            this.orderDetialBean = orderDetialBean;
            tvOriderMoney.setText(AlgorithmUtil.getAmount(orderDetialBean.getOrderAmount() + "", 100) + "元");
            payResultMode.IsPrintTicket(getContext(), orderDetialBean, kind);
            tvPrintTwo.setVisibility(View.VISIBLE);
        });
        payResultMode.getOrderDetail(getContext(), getArguments().getString("orderNumber"));
    }

    @OnClick({R.id.tv_print_two})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_print_two:
                switch (kind) {
                    case 0://操作员收费打印小票
                        PrintHelper.getInstance().PrintOperatorChargeTicket(orderDetialBean, true);
                        tvPrintTwo.setVisibility(View.GONE);
                        break;
                    case 1://操作员会员消费打印小票
                        payResultMode.printMemberPay(orderDetialBean,true);
                        tvPrintTwo.setVisibility(View.GONE);
                        break;

                }

                break;
        }
    }
}
